package com.gl.emms.whale.firstrun;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.gl.emms.whale.R;
import com.gl.emms.whale.app.Constant;
import com.gl.emms.whale.ui.LoginActivity;
import com.gl.emms.whale.ui.RegisterActivity;
import com.gl.emms.whale.ui.SplanshActivity;
import com.gl.emms.whale.util.SharePreferenceUtil;

import android.app.Activity;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class ScrollLayoutActivity extends Activity implements OnViewChangeListener{

	private ScrollLayout mScrollLayout;
	private ImageView[] imgs;
	private int count;
	private int currentItem;
	private Button startBtn,ghl2Btn,fxlBtn;
	private RelativeLayout mainRLayout;
	private LinearLayout pointLLayout;
	private LinearLayout leftLayout;
	private LinearLayout rightLayout;
	private LinearLayout animLayout;

	private SharePreferenceUtil util;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		util = new SharePreferenceUtil(this, Constant.SAVE_USERINFO);
		//if (true) {
		if (util.getisFirst()) {
			setContentView(R.layout.activity_firstrun);
			initView();
			createShut();
			moveSound();
		}else
		{
			Intent intent = new Intent(
					ScrollLayoutActivity.this,
					SplanshActivity.class);
			ScrollLayoutActivity.this.startActivity(intent);
			ScrollLayoutActivity.this.finish();
			overridePendingTransition(R.anim.zoom_out_enter,R.anim.zoom_out_exit);
		}

	}

	private void initView() {
		mScrollLayout = (ScrollLayout) findViewById(R.id.ScrollLayout);
		pointLLayout = (LinearLayout) findViewById(R.id.llayout);
		mainRLayout = (RelativeLayout) findViewById(R.id.mainRLayout);
		startBtn = (Button) findViewById(R.id.startBtn);
		startBtn.setOnClickListener(onClick);

		ghl2Btn = (Button) findViewById(R.id.ghl2Btn);
		ghl2Btn.setOnClickListener(onClick);

		fxlBtn = (Button) findViewById(R.id.fxlBtn);
		fxlBtn.setOnClickListener(onClick);

		animLayout = (LinearLayout) findViewById(R.id.animLayout);
		leftLayout = (LinearLayout) findViewById(R.id.leftLayout);
		rightLayout = (LinearLayout) findViewById(R.id.rightLayout);
		count = mScrollLayout.getChildCount();
		imgs = new ImageView[count];
		for (int i = 0; i < count; i++) {
			imgs[i] = (ImageView) pointLLayout.getChildAt(i);
			imgs[i].setEnabled(true);
			imgs[i].setTag(i);
		}
		currentItem = 0;
		imgs[currentItem].setEnabled(false);
		mScrollLayout.SetOnViewChangeListener(this);
	}

	private View.OnClickListener onClick = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.startBtn:


				mScrollLayout.setVisibility(View.GONE);
				pointLLayout.setVisibility(View.GONE);
				animLayout.setVisibility(View.VISIBLE);

				mainRLayout.setBackgroundResource(R.drawable.firstrun1);
				//初始化  
				Animation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);  
				//设置动画时间           
				alphaAnimation.setDuration(2000);  
				mainRLayout.startAnimation(alphaAnimation);

				Animation leftOutAnimation = AnimationUtils.loadAnimation(
						getApplicationContext(), R.anim.translate_left);
				Animation rightOutAnimation = AnimationUtils.loadAnimation(
						getApplicationContext(), R.anim.translate_right);
				leftLayout.setAnimation(leftOutAnimation);
				rightLayout.setAnimation(rightOutAnimation);
				leftOutAnimation.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {
						//mainRLayout.setBackgroundColor(Color.BLACK);

					}

					@Override
					public void onAnimationRepeat(Animation animation) {
					}

					@Override
					public void onAnimationEnd(Animation animation) {
						leftLayout.setVisibility(View.GONE);
						rightLayout.setVisibility(View.GONE);
						Intent intent = new Intent(
								ScrollLayoutActivity.this,
								SplanshActivity.class);
						ScrollLayoutActivity.this.startActivity(intent);
						ScrollLayoutActivity.this.finish();
						//������Activity������Activity֮ǰ��һ����ȶ���
						overridePendingTransition(R.anim.zoom_out_enter,R.anim.zoom_out_exit);
					}
				});

				break;
			case R.id.ghl2Btn:

				mScrollLayout.setVisibility(View.GONE);
				pointLLayout.setVisibility(View.GONE);
				animLayout.setVisibility(View.VISIBLE);

				mainRLayout.setBackgroundResource(R.drawable.firstrun1);
				//初始化  
				Animation alphaAnimation1 = new AlphaAnimation(0.1f, 1.0f);  
				//设置动画时间           
				alphaAnimation1.setDuration(2000);  
				mainRLayout.startAnimation(alphaAnimation1);

				Animation leftOutAnimation1 = AnimationUtils.loadAnimation(
						getApplicationContext(), R.anim.translate_left);
				Animation rightOutAnimation1 = AnimationUtils.loadAnimation(
						getApplicationContext(), R.anim.translate_right);
				leftLayout.setAnimation(leftOutAnimation1);
				rightLayout.setAnimation(rightOutAnimation1);
				leftOutAnimation1.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {
						//mainRLayout.setBackgroundColor(Color.BLACK);
					}

					@Override
					public void onAnimationRepeat(Animation animation) {
					}

					@Override
					public void onAnimationEnd(Animation animation) {
						leftLayout.setVisibility(View.GONE);
						rightLayout.setVisibility(View.GONE);
						Intent intent = new Intent(
								ScrollLayoutActivity.this,
								SplanshActivity.class);
						ScrollLayoutActivity.this.startActivity(intent);
						ScrollLayoutActivity.this.finish();
						//������Activity������Activity֮ǰ��һ����ȶ���
						overridePendingTransition(R.anim.zoom_out_enter,R.anim.zoom_out_exit);
					}
				});

				break;
			case R.id.fxlBtn:

				mScrollLayout.setVisibility(View.GONE);
				pointLLayout.setVisibility(View.GONE);
				animLayout.setVisibility(View.VISIBLE);

				mainRLayout.setBackgroundResource(R.drawable.firstrun1);
				//初始化  
				Animation alphaAnimation2 = new AlphaAnimation(0.1f, 1.0f);  
				//设置动画时间           
				alphaAnimation2.setDuration(2000);  
				mainRLayout.startAnimation(alphaAnimation2);

				Animation leftOutAnimation2 = AnimationUtils.loadAnimation(
						getApplicationContext(), R.anim.translate_left);
				Animation rightOutAnimation2 = AnimationUtils.loadAnimation(
						getApplicationContext(), R.anim.translate_right);
				leftLayout.setAnimation(leftOutAnimation2);
				rightLayout.setAnimation(rightOutAnimation2);
				leftOutAnimation2.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {
						//mainRLayout.setBackgroundColor(Color.BLACK);
					}

					@Override
					public void onAnimationRepeat(Animation animation) {
					}

					@Override
					public void onAnimationEnd(Animation animation) {
						leftLayout.setVisibility(View.GONE);
						rightLayout.setVisibility(View.GONE);
						Intent intent = new Intent(
								ScrollLayoutActivity.this,
								SplanshActivity.class);
						ScrollLayoutActivity.this.startActivity(intent);
						ScrollLayoutActivity.this.finish();
						//������Activity������Activity֮ǰ��һ����ȶ���
						overridePendingTransition(R.anim.zoom_out_enter,R.anim.zoom_out_exit);
					}
				});

				break;
			}
		}
	};

	@Override
	public void OnViewChange(int position) {
		setcurrentPoint(position);
	}

	private void setcurrentPoint(int position) {
		if (position < 0 || position > count - 1 || currentItem == position) {
			return;
		}
		imgs[currentItem].setEnabled(true);
		imgs[position].setEnabled(false);
		currentItem = position;
	}
	/**
	 * 创建桌面快捷方式
	 */
	
	public void createShut() {
		// 创建添加快捷方式的Intent
		Intent addIntent = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");
		String title = getResources().getString(R.string.app_name);
		// 加载快捷方式的图标
		Parcelable icon = Intent.ShortcutIconResource.fromContext(
				ScrollLayoutActivity.this, R.drawable.ic_launcher);
		// 创建点击快捷方式后操作Intent,该处当点击创建的快捷方式后，再次启动该程序
		Intent myIntent = new Intent(ScrollLayoutActivity.this,
				SplanshActivity.class);
		// 设置快捷方式的标题
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
		// 设置快捷方式的图标
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
		// 设置快捷方式对应的Intent
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);
		// 发送广播添加快捷方式
		sendBroadcast(addIntent);
		util.setIsFirst(false);
	}
	 
	
	/**
	 * 复制原生资源文件“来消息声音”到应用目录下，
	 */
	public void moveSound() {
		InputStream is = getResources().openRawResource(R.raw.msg);
		File file = new File(getFilesDir(), "msg.mp3");
		try {
			OutputStream os = new FileOutputStream(file);
			int len = -1;
			byte[] buffer = new byte[1024];
			while ((len = is.read(buffer)) != -1) {
				os.write(buffer, 0, len);
			}
			//			System.out.println("声音复制完毕");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}